package com.hafu.view.GYS;

import com.hafu.model.GysInfo;
import com.hafu.service.GysInfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author 111
 */
public class ManageGYSFrm extends JFrame {
    private GysInfoService gysInfoService = new GysInfoService();

    public ManageGYSFrm() {
        initComponents();
        fillTable();
    }

    private void fillTable() {
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoByName(new GysInfo());
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        for (int i = 0; i < gysInfoList.size(); i++) {
            GysInfo gysInfo = gysInfoList.get(i);
            Vector<Object> rowData = new Vector<>();
            rowData.add(i + 1); // 编号为索引值加一，显示为从1开始
            rowData.add(gysInfo.getName());
            rowData.add(gysInfo.getTel());
            model.addRow(rowData);
        }
    }

    private void button1(ActionEvent e) {
        String name = textField1.getText();
        GysInfo gysInfo = new GysInfo();
        gysInfo.setName(name);
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoByName(gysInfo);
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        if (gysInfoList.size() > 0) {
            for (int i = 0; i < gysInfoList.size(); i++) {
                GysInfo gysInfo1 = gysInfoList.get(i);
                Vector<Object> rowData = new Vector<>();
                rowData.add(i + 1); // 编号为索引值加一，显示为从1开始
                rowData.add(gysInfo1.getName());
                rowData.add(gysInfo1.getTel());
                model.addRow(rowData);
            }
        } else {
            JOptionPane.showMessageDialog(null, "没有查询到该供应商信息");
        }
    }

    private void table1MouseClicked(MouseEvent e) {
        int row = table1.getSelectedRow();
        if (row != -1) {
            DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
            String name = (String) dtm.getValueAt(row, 1); // 名称在第二列
            String tel = (String) dtm.getValueAt(row, 2); // 电话在第三列
            textField2.setText(name);
            textField3.setText(tel);
        }
    }

    private void button2(ActionEvent e) {
        String name = textField2.getText();
        if (StringUtil.isEmpty(name)) {
            JOptionPane.showMessageDialog(null, "供应商名称不能为空");
            return;
        }
        String tel = textField3.getText();
        if (StringUtil.isEmpty(tel)) {
            JOptionPane.showMessageDialog(null, "供应商电话不能为空");
            return;
        }
        int row = table1.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要修改的供应商");
            return;
        }
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chosenId = getActualIdFromTableRow(row); // 获取实际ID
        GysInfo gysInfo = new GysInfo(chosenId, name, tel);
        boolean aBoolean = gysInfoService.updateGysInfo(gysInfo);
        if (aBoolean) {
            JOptionPane.showMessageDialog(null, "修改成功");
            dtm.setValueAt(name, row, 1);
            dtm.setValueAt(tel, row, 2);
        } else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }

    private void button3(ActionEvent e) {
        int row = table1.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请选择要删除的供应商");
            return;
        }
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chosenId = getActualIdFromTableRow(row); // 获取实际ID
        boolean isDeleteGysInfo = gysInfoService.deleteGysInfo(chosenId);
        if (isDeleteGysInfo) {
            JOptionPane.showMessageDialog(null, "删除成功");
            dtm.removeRow(row);
        } else {
            JOptionPane.showMessageDialog(null, "删除失败");
        }
    }

    private void table1PropertyChange(PropertyChangeEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button1 = new JButton();
        label2 = new JLabel();
        label3 = new JLabel();

        //======== this ========
        setTitle("管理供应商");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("供应商名称");

        //---- button2 ----
        button2.setText("修改");
        button2.addActionListener(e -> button2(e));

        //---- button3 ----
        button3.setText("删除");
        button3.addActionListener(e -> button3(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null},
                            {null, null, null},
                    },
                    new String[] {
                            "编号", "名称", "电话"
                    }
            ) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false; // 禁止编辑表格内容
                }
            });
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            table1.addPropertyChangeListener(e -> table1PropertyChange(e));
            scrollPane1.setViewportView(table1);
        }

        //---- button1 ----
        button1.setText("查询");
        button1.addActionListener(e -> button1(e));

        //---- label2 ----
        label2.setText("供应商名称");

        //---- label3 ----
        label3.setText("供应商电话");

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(131, 131, 131)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(label2)
                                                        .addComponent(label3))
                                                .addGap(69, 69, 69)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
                                                .addGap(132, 132, 132)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(button3)
                                                        .addComponent(button2))
                                                .addContainerGap(111, Short.MAX_VALUE))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(1, 1, 1)
                                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                                .addGap(50, 50, 50)
                                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 147, Short.MAX_VALUE)
                                                .addComponent(button1)
                                                .addGap(109, 109, 109))))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                .addContainerGap(95, Short.MAX_VALUE)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 546, GroupLayout.PREFERRED_SIZE)
                                .addGap(92, 92, 92))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(24, 24, 24)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button1))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 264, GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label2)
                                        .addComponent(button2))
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label3)
                                        .addComponent(button3))
                                .addGap(79, 79, 79))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // 新增方法：获取实际ID
    private int getActualIdFromTableRow(int rowIndex) {
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        String name = (String) model.getValueAt(rowIndex, 1); // 名称在第二列
        String tel = (String) model.getValueAt(rowIndex, 2); // 电话在第三列
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoByName(new GysInfo(name, tel));
        if (!gysInfoList.isEmpty()) {
            return gysInfoList.get(0).getId();
        }
        return -1; // 如果未找到，返回-1或其他适当的错误码
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JButton button2;
    private JButton button3;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button1;
    private JLabel label2;
    private JLabel label3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
